package com.example.reagent_management.ui.message.pojo

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.PagingSource
import com.example.reagent_management.room.dao.MessageDao
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.flowOf
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class MessageRepository @Inject constructor(private val messageDao: MessageDao) {

    fun insertMessage(message: Message): Long {
        return messageDao.insertMessage(message)
    }

    /**
     * 获取所有的通知  pagingSource -> PagingData -> Flow/LiveData
     * 数据源 到 可观察的数据
     */
    fun getALlMessage(): Flow<PagingData<Message>> {
        return Pager(config = PagingConfig(pageSize = 30, maxSize = 150, enablePlaceholders = false),
            pagingSourceFactory = { messageDao.allMessage }
        ).flow
    }

    /**
     * 获取是否已经提醒过
     */
    fun getRemind(type:Int,remindId:Long):Long{
        return messageDao.getRemind(type,remindId)?:-1L
    }

    /**
     * 删除所有消息
     */
    fun deleteALL(){
        messageDao.deleteAll()
    }

    /**
     * 清理
     */
    fun cleanMessage(){
        messageDao.cleanMessage()
    }
}